package yf.mall.controller.api;

import com.yf.annotation.IgnoreAuth;
import com.yf.annotation.LoginUserId;
import com.yf.response.R;
import com.yf.response.RRException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import yf.mall.common.Const;
import yf.mall.dao.*;
import yf.mall.entity.*;
import yf.mall.enums.*;
import yf.mall.service.*;
import yf.mall.vo.LotteryActVo;
import yf.mall.vo.LotteryCountNumberVo;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RequestMapping("/api/lottery")
@RestController
public class ApiLotteryController {

    @Autowired
    private LotteryActService lotteryActService;

    @Autowired
    private LotteryGoodsService lotteryGoodsService;

    @Autowired
    private LotteryRecordService lotteryRecordService;

    @Autowired
    private LotteryCountService lotteryCountService;

    @Autowired
    private UserService userService;

    /**
     * 查处所有开启的抽奖活动取第一个
     *
     * @param userId
     * @return
     */
    @RequestMapping("act")
    public R lotteryAct(@LoginUserId Long userId, Long actId) {
        if (actId == null) {
            throw new RRException("没有抽奖活动");
        }
        LotteryActEntity lotteryActEntity = lotteryActService.get(actId);
        if (lotteryActEntity == null) {
            throw new RRException("暂未开启的活动");
        }
        Date date = new Date();
        SimpleDateFormat smp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 判断活动是否开始
        if (date.before(lotteryActEntity.getStartTime())) {
            throw new RRException("活动未开始,将于" + smp.format(lotteryActEntity.getStartTime()) + "开始");
        }
        if (date.after(lotteryActEntity.getEndTime())) {
            throw new RRException("活动已结束");
        }
        return R.ok(lotteryActEntity);
    }

    @Autowired
    private LotteryRecordDao lotteryRecordDao;

    /**
     * 开始抽奖
     *
     * @param userId
     * @param actId
     * @return
     */
    @RequestMapping("start")
    public R startLottery(@LoginUserId Long userId, Long actId, Long recordId) {
        if (actId .equals( null)||recordId.equals(null)) {
            throw new RRException("没有抽奖活动");
        }
        // 获取活动
        LotteryActEntity lotteryActEntity = lotteryActService.get(actId);
        Date date = new Date();
        SimpleDateFormat smp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (date.before(lotteryActEntity.getStartTime())) {
            throw new RRException("活动未开始,将于" + smp.format(lotteryActEntity.getStartTime()) + "开始");
        }
        if (date.after(lotteryActEntity.getEndTime())) {
            throw new RRException("活动已结束");
        }

        LotteryGoodsEntity lotteryGoodsEntity = this.lotteryActService.startLottery(userId, actId, recordId);
        UserEntity userEntity = userService.get(userId);
        return R.ok(lotteryGoodsEntity).putExtra("userInfo",userEntity.getCode()+userEntity.getName());
    }

    /**
     * 抽奖明细
     *
     * @param userId
     * @return
     */
    @RequestMapping("allLotteryInfo")
    public R allLotteryInfo(@LoginUserId Long userId) {
        List<LotteryRecordEntity> recordEntities = this.lotteryRecordService.allLotteryInfo(userId);

        return R.ok(recordEntities);
    }


}
